pipeline {
    agent any
    environment {
        HARBOR_REG = "http://192.168.10.144:5000"  // 替换为你的 Harbor 地址
        IMAGE_NAME = "192.168.10.144:5000/test/jenkins-docker-java"   // 替换为你的私服镜像名称
        HARBOR_CREDENTIALID = '66dd3fa3-0cb4-4837-8d05-7258c438c819'  // 替换为你的Harbor认证
        GIT_CREDENTIALID = '1f176d1a-f780-4d2a-85b8-8185538b042f'  // 替换为你的GIT认证
        GIT_URL = 'http://192.168.10.91:8888/test/helloworld.git'  // 替换为你的项目GIT地址
		REMOTE_SERVER_URL = '192.168.10.177'
    }
    stages {
        stage('拉取代码') {
            steps {
                checkout scmGit(branches: [[name: "${branch}"]], extensions: [], userRemoteConfigs: [[credentialsId: "${GIT_CREDENTIALID}", url: "${GIT_URL}"]])
            }
        }
        stage('mvn打jar包') {
            steps {
                sh '''
                    /opt/maven/apache-maven-3.9.6/bin/mvn install -am -Dmaven.test.skip=true
                '''
            }
        }
         stage('Build Image') {
            steps {
                script {
                    // 动态生成标签（例：v1.0.${BUILD_NUMBER}）
                    tag = "v1"
                    // 构建并推送镜像
                    docker.build("${IMAGE_NAME}:${tag}")
                    docker.withRegistry("${HARBOR_REG}", "${HARBOR_CREDENTIALID}") {
                        docker.image("${IMAGE_NAME}:${tag}").push()
                    }
                }
            }
        }
        stage('触发目标服务器启动容器') {
            steps {
                sshPublisher(publishers: [sshPublisherDesc(configName: "${REMOTE_SERVER_URL}", transfers: [sshTransfer(cleanRemote: false, excludes: '', execCommand: 'cd /usr/local/jenkins-docker && sh docker-deploy.sh', execTimeout: 120000, flatten: false, makeEmptyDirs: false, noDefaultExcludes: false, patternSeparator: '[, ]+', remoteDirectory: '', remoteDirectorySDF: false, removePrefix: '', sourceFiles: '')], usePromotionTimestamp: false, useWorkspaceInPromotion: false, verbose: false)])
            }
        }
    }
}
